package com.PAT.UnSatisfying;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zjz
 * @Date: 2021/12/21/21:47
 * @Description:
 * @Target: 差10
 */
public class 链表元素分类 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        String head = in.next();
        int len = in.nextInt();
        int K = in.nextInt();

        LinkNode[] LinkNodes = new LinkNode[len];
        for (int i = 0; i < len; i++) {
            String address = in.next();
            int data  = in.nextInt();
            String next = in.next();
            LinkNode LinkNode = new LinkNode(address,data,next);
            LinkNodes[i] = LinkNode;
        }

        LinkedList<LinkNode> list = new LinkedList();
        ArrayList<LinkNode>  arrayList = new ArrayList();

        int y = 0;
        for (LinkNode LinkNode : LinkNodes) {
            if (LinkNode.Address.equals(head)){
                if (LinkNode.getData()<0){
                    y++;
                }
                list.add(LinkNode);
            }
        }

        boolean flag = false;
        while (list.size()>0&&list.size()<len&&flag==false){
            for (LinkNode LinkNode : LinkNodes) {
                if (list.get(list.size()-1).getNext().equals(LinkNode.getAddress())){
                    if (LinkNode.getData()<0){
                        y++;
                    }
                    list.add(LinkNode);
                    if (LinkNode.getNext().equals("-1")){
                        flag = true;
                    }
                }
            }
        }

        int x =0;
        for (LinkNode linkNode : list) {
            if (linkNode.getData()>K){
                arrayList.add(linkNode);
            }else if (linkNode.getData()<0){
                LinkNodes[x++]=linkNode;
            }else {
                LinkNodes[y++]=linkNode;
            }
        }

/*        System.out.println(list.size());
        list.forEach(System.out::println);

        Arrays.sort(LinkNodes,new MyCompara());*/



        for (int i = 0; i < y; i++) {
            if (i==0){
                System.out.print(LinkNodes[i].Address + " "+ LinkNodes[i].getData());
            }else {
                System.out.print(" "+LinkNodes[i].Address);
                System.out.println();
                System.out.print(LinkNodes[i].Address + " "+ LinkNodes[i].getData());
            }


        }


        for (int i = 0; i < arrayList.size(); i++) {
            if (i==arrayList.size()-1){
                System.out.print(" "+arrayList.get(i).Address);
                System.out.println();
                System.out.print(arrayList.get(i).Address + " "+ arrayList.get(i).getData());
                System.out.print(" -1");
            }else {
                System.out.print(" " + arrayList.get(i).Address);
                System.out.println();
                System.out.print(arrayList.get(i).Address + " "+ arrayList.get(i).getData());
            }
        }

    }




    static class MyCompara implements Comparator<LinkNode>{
        @Override
        public int compare(LinkNode o1, LinkNode o2) {
            return o1.getData()- o2.getData();
        }
    }

    static class LinkNode{
        private String Address;
        private int Data;
        private String Next;

        public LinkNode() {
        }

        public LinkNode(String address, int data, String next) {
            Address = address;
            Data = data;
            Next = next;
        }

        @Override
        public String toString() {
            return "LinkNode{" +
                    "Address='" + Address + '\'' +
                    ", Data=" + Data +
                    ", Next='" + Next + '\'' +
                    '}';
        }

        public String getAddress() {
            return Address;
        }

        public void setAddress(String address) {
            Address = address;
        }

        public int getData() {
            return Data;
        }

        public void setData(int data) {
            Data = data;
        }

        public String getNext() {
            return Next;
        }

        public void setNext(String next) {
            Next = next;
        }
    }
}
